package com.yunpuvip.sachet.controller;

import com.google.zxing.qrcode.encoder.QRCode;
import com.yunpuvip.sachet.biz.entity.Order;
import com.yunpuvip.sachet.biz.entity.Payment;
import com.yunpuvip.sachet.biz.model.params.OrderParam;
import com.yunpuvip.sachet.biz.service.OrderService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.yunpuvip.sachet.biz.service.SysparamService;
import com.yunpuvip.sachet.config.properties.GunsProperties;
import com.yunpuvip.sachet.modular.constant.ApplicationConstant;
import com.yunpuvip.sachet.modular.factory.QrCodeFactory;
import com.yunpuvip.sachet.modular.page.LayuiPageInfo;
import com.yunpuvip.sachet.modular.system.factory.BeanToListMapFactory;
import com.yunpuvip.sachet.modular.vo.rest.request.RechargeVo;
import com.yunpuvip.sachet.modular.vo.rest.response.RechargePrePayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;


/**
 * 订单信息控制器
 *
 * @author wushaohong
 * @Date 2019-08-10 09:46:09
 */
@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {

    private String PREFIX = "/modular/order";

    @Autowired
    private OrderService orderService;

    @Autowired
    private SysparamService sysparamService;

    @Autowired
    private GunsProperties gunsProperties;
    /**
     * 跳转到主页面
     *
     * @author wushaohong
     * @Date 2019-08-10
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/order.html";
    }

    /**
     * 新增页面
     *
     * @author wushaohong
     * @Date 2019-08-10
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/order_add.html";
    }

    /**
     * 编辑页面
     *
     * @author wushaohong
     * @Date 2019-08-10
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/order_edit.html";
    }

    /**
     * 新增接口
     *
     * @author wushaohong
     * @Date 2019-08-10
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(OrderParam orderParam) {
        Integer playerId = orderParam.getPlayerId();
        RechargeVo rechargeVo = new RechargeVo();

        rechargeVo.setAmount(orderParam.getTotalPrice());
        rechargeVo.setRemark(orderParam.getRemark());
        rechargeVo.setExpireDate(orderParam.getExpireDate());
        rechargeVo.setProduceCode(orderParam.getProduceCode());
        /*String fileUploadPath = gunsProperties.getFileUploadPath();
        rechargeVo.setUploadPath(fileUploadPath);*/
        RechargePrePayVo rechargePrePayVo = this.orderService.userOrderPack(playerId, rechargeVo);
        return ResponseData.success();
    }


    /**
     * 下载二维码
     *
     * @author wushaohong
     * @Date 2019-08-10
     */
    @RequestMapping("/downloadQr")
    @ResponseBody
    public void  downloadQr(Integer orderId, HttpServletResponse response) throws IOException {
        String qrUrl = sysparamService.getSysparamVlaue(ApplicationConstant.QRCODE);
        qrUrl+=orderId;
        byte[] qrcode = QrCodeFactory.createQrcode(qrUrl);
        try {
            //设置字符集和文件后缀名
            response.setContentType("application/png" +  ";" +"charset = UTF-8");
            long name = System.currentTimeMillis();

            //name = new String(name.getBytes(), "ISO-8859-1");
            // 设置文件名称
            response.setHeader("Content-Disposition","attachment; filename=" +name+".png");
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            toClient.write(qrcode);
            toClient.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 编辑接口
     *
     * @author wushaohong
     * @Date 2019-08-10
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(OrderParam orderParam) {
        this.orderService.update(orderParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author wushaohong
     * @Date 2019-08-10
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(OrderParam orderParam) {
        this.orderService.delete(orderParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author wushaohong
     * @Date 2019-08-10
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(OrderParam orderParam) {
        Order detail = this.orderService.getById(orderParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author wushaohong
     * @Date 2019-08-10
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(OrderParam orderParam) {

        LayuiPageInfo pageBySpec = this.orderService.findPageBySpec(orderParam);
        return pageBySpec;
        //List<Map<String, Object>> maps = BeanToListMapFactory.toListMap(pageBySpec);

    }

}


